<template>
  <div class="collapsible-panel">
    <div class="panel-header" @click="toggleCollapse">
      <slot name="header"></slot>
      <el-icon>
        <template v-if="collapsed">
          <ArrowUp />
        </template>
        <template v-else> <ArrowDown /> </template
      ></el-icon>
    </div>
    <transition name="fade">
      <div v-if="collapsed" class="panel-body">
        <slot name="body"></slot>
      </div>
    </transition>
  </div>
</template>

<script setup>
const emit = defineEmits(["toggleCollapse"]);
// 折叠状态
const collapsed = ref(false);
// 切换折叠状态
const toggleCollapse = () => {
  collapsed.value = !collapsed.value;
  emit("toggleCollapse", collapsed.value); // 额外事件，用于其他用途
};
</script>
<style scoped lang="scss">
.collapsible-panel {
  border-radius: 4px;
  margin-bottom: 10px;
}
.panel-header {
  padding: 5px 10px;
  cursor: pointer;
  user-select: none;
  background-color: #f5f5f5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background-color 0.3s ease;
  &:hover {
    background-color: #e0e0e0;
  }
}
.icon {
  transition: transform 0.3s ease;
}
.panel-body {
  padding: 10px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
